package com.msb.serviceprice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.msb.internalcommon.constant.CommonStatusEnum;
import com.msb.internalcommon.dto.PriceRule;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.serviceprice.mapper.PriceRuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.Response;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: jiudianliu
 * @Date: 2023/7/18 - 07 - 18 - 16:07
 * @Description: com.msb.serviceprice.service
 * @version: 1.0
 */
@Service
public class PriceRuleService {

    @Autowired
    private PriceRuleMapper priceRuleMapper ;

    public ResponseResult add(PriceRule priceRule) {

        // 拼接fareType
        String cityCode = priceRule.getCityCode() ;
        String vehicleType = priceRule.getVehicleType() ;
        String fareType = cityCode + "$" + vehicleType ;
        priceRule.setFareType(fareType) ;

        // 添加版本号
        QueryWrapper<PriceRule> queryWrapper = new QueryWrapper<>() ;
        queryWrapper.eq("city_code",cityCode) ;
        queryWrapper.eq("vehicle_type",vehicleType);
        // 按照版本号排序
        queryWrapper.orderByDesc("fare_version");

        // 查询
        List<PriceRule> priceRules = priceRuleMapper.selectList(queryWrapper);
        Integer fareVersion = 0 ;
        if (priceRules.size() > 0){
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EXISTS.getCode() , CommonStatusEnum.PRICE_RULE_EXISTS.getValue());
        }
        priceRule.setFareVersion(++fareVersion);

        priceRuleMapper.insert(priceRule);
        return ResponseResult.success();
    }

    public ResponseResult edit(PriceRule priceRule){
        // 拼接fareType
        String cityCode = priceRule.getCityCode() ;
        String vehicleType = priceRule.getVehicleType() ;
        String fareType = cityCode + "$" + vehicleType ;
        priceRule.setFareType(fareType) ;

        // 添加版本号
        QueryWrapper<PriceRule> queryWrapper = new QueryWrapper<>() ;
        queryWrapper.eq("city_code",cityCode) ;
        queryWrapper.eq("vehicle_type",vehicleType);
        // 按照版本号排序
        queryWrapper.orderByDesc("fare_version");

        // 查询
        List<PriceRule> priceRules = priceRuleMapper.selectList(queryWrapper);
        Integer fareVersion = 0 ;
        if (priceRules.size() > 0){
            PriceRule priceRuleList = priceRules.get(0);
            Double unitPricePerMile = priceRuleList.getUnitPricePerMile();
            Double unitPricePerMinute = priceRuleList.getUnitPricePerMinute();
            Double startFare = priceRuleList.getStartFare();
            Integer startMile = priceRuleList.getStartMile();

            if (unitPricePerMile.doubleValue() == priceRule.getUnitPricePerMile().doubleValue()
            && unitPricePerMinute.doubleValue() == priceRule.getUnitPricePerMinute().doubleValue()
            && startFare.doubleValue() == priceRule.getStartFare().doubleValue()
            && startMile.intValue() == priceRule.getStartMile().intValue()){
                return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_NOT_EDIT.getCode() , CommonStatusEnum.PRICE_RULE_NOT_EDIT.getValue());
            }

            fareVersion = priceRules.get(0).getFareVersion();
        }
        priceRule.setFareVersion(++fareVersion);

        priceRuleMapper.insert(priceRule);
        return ResponseResult.success();
    }

    public ResponseResult<Boolean> ifExists(PriceRule priceRule) {
        // 获取城市编码以及车辆类型
        String cityCode = priceRule.getCityCode();
        String vehicleType = priceRule.getVehicleType();

        QueryWrapper<PriceRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_code",cityCode);
        queryWrapper.eq("vehicle_type",vehicleType);
        queryWrapper.orderByDesc("fare_version");

        List<PriceRule> priceRuleList = priceRuleMapper.selectList(queryWrapper);
        if (priceRuleList.size() > 0) {
            return ResponseResult.success(true);
        }else{
            return ResponseResult.success(false);
        }

    }
}
